package com.xym.manage.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xym.common.bean.ItemCatResult;
import com.xym.manage.service.ItemCatService;

@Controller
@RequestMapping("api/itemcat")
public class ApiItemCatController {

	@Autowired
	private ItemCatService itemCatService;
	
	/**
	 * 接口  调用  查询分类三级菜单
	 * @param callback
	 * @return
	 */
	@RequestMapping(value="/all",method=RequestMethod.GET)
	public ResponseEntity<ItemCatResult> queryItemCatAll(){
		try {
			ItemCatResult itemCatResult=itemCatService.queryApi_ItemCatResult();
			
			//不论是不是跨域请求，都调用     xym-manage-context.xml中配置
			//了消息控制器com.xym.common.CallbackMappingJackson2HttpMessageConverter
			return ResponseEntity.status(HttpStatus.OK).body(itemCatResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	
	/**
	 * 接口  调用  查询分类三级菜单
	 * @param callback
	 * @return
	 */
	/*
	@RequestMapping(value="/all",method=RequestMethod.GET)
	public ResponseEntity<String> queryItemCatAll(@RequestParam(value="callback", required = false)String callback){
		
		try {
			ItemCatResult itemCatResult=itemCatService.queryApi_ItemCatResult();
			
			//对象转化为json
			ObjectMapper mapper = new ObjectMapper();
			String jsonString = mapper.writeValueAsString(itemCatResult);
			
			//如果是跨域请求,调用
			if(!StringUtils.isEmpty(callback)){
				return ResponseEntity.status(HttpStatus.OK).body(callback+"("+jsonString+")");
			}
			
			//不是跨域请求，调用
			return ResponseEntity.status(HttpStatus.OK).body(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}*/
}
